package com.zh.kotlin_mvvm.provider

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.util.Log
import com.zh.kotlin_mvvm.provider.AdProviderUtils

internal class MyDatabaseHelper : SQLiteOpenHelper {

    private val tableName = AdProviderUtils.instance.table
    private val createAds = "create table $tableName (" +
            "${AdProviderUtils.instance.DB_ID_NAME} INTEGER primary key autoincrement," +
            "${AdProviderUtils.instance.DB_ADID_NAME} VARCHAR NOT NULL," +
            "${AdProviderUtils.instance.DB_TIME_NAME} LONG NOT NULL)"

    constructor(context: Context?) : super(context, "adsStore.db", null, 4)

    override fun onCreate(db: SQLiteDatabase?) {
        Log.i("MyDatabaseHelper", "onCreate---")
        db?.execSQL(createAds)
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        Log.i("MyDatabaseHelper", "onUpgrade---")
        val sql = "ALTER TABLE $tableName ADD COLUMN ${AdProviderUtils.instance.DB_DISTYPE_NAME} INTEGER DEFAULT 0"
        db?.execSQL(sql)
    }
}